package com.huawei.codearts.tools.exception;

import com.huawei.codearts.tools.validated.StatusEnum;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Objects;

public class ExceptionUtil {

    public static void rethrowCustomException(int code, String msg) {
        throw new CustomException(code, msg);
    }

    public static void rethrowCustomException(String msg) {
        throw new CustomException(msg);
    }

    public static void rethrowServiceException(StatusEnum status, String message) {
        throw new ServiceException(status, message);
    }

    public static void rethrowServiceException(StatusEnum status) {
        throw new ServiceException(status);
    }

    public static String extractStackTrace(Throwable error) {
        if (Objects.isNull(error)) {
            return "";
        }
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {
            error.printStackTrace(pw);
            sw.flush();
            return sw.toString();
        } finally {
            pw.close();
        }
    }

}
